/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __PQ_RigidGameObject__
#define __PQ_RigidGameObject__

#include "EnginePrerequisites.h"

#include "BulletDynamics/Dynamics/btRigidBody.h"
#include "BulletCollision/CollisionShapes/btCollisionShape.h"
#include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
#include "BulletCollision/CollisionShapes/btBoxShape.h"
#include "BulletCollision/CollisionShapes/btSphereShape.h"
#include "BulletCollision/CollisionShapes/btConeShape.h"

#include "ColliderComponent.h"

#include "MathUtil.h"
#include "GameObjectState.h"

namespace PQEngine
{
	//manage game object physics
	class RigidGameObject
	{
	protected:
		GameObject* _gameObject;
		GameObjectState* _objectState;

		btCollisionShape* _collisionShape;
		btRigidBody* _rigidBody;
	public:
		RigidGameObject(GameObject* object);
		void initCollisionShape();
		void initRigidBody();
		void initPlaneShape(PlaneColliderComponent* collider);
		void initBoxShape(BoxColliderComponent* collider);
		void initSphereShape(SphereColliderComponent* collider);

		btRigidBody* getRigidBody();

		bool isKinematicObject();
		void setKinematicObject(bool isKinematic);

		void setLinearVelocity(const Ogre::Vector3 &v);
		Ogre::Vector3 getLinearVelocity();

		void applyImpulse(const Ogre::Vector3 &impulse, const Ogre::Vector3 &position);
		void applyForce(const Ogre::Vector3 &impulse, const Ogre::Vector3 &position);

		Ogre::Vector3 getCenterOfMassPivot(const Ogre::Vector3 &pivotPosition) const;
		void setDeactivationTime(const float ftime);

		void setDamping(const float linearDamping, const float angularDamping);

		inline const btTransform &getCenterOfMassTransform() const
		{
			return _rigidBody->getCenterOfMassTransform();
		}

		inline Ogre::Quaternion getCenterOfMassOrientation() const
		{
			return MathUtil::toOgre(_rigidBody->getCenterOfMassTransform().getRotation());
		}

		inline Ogre::Vector3 getCenterOfMassPosition() const
		{
			return MathUtil::toOgre(_rigidBody->getCenterOfMassPosition());
		}

	};
}
#endif
